package com.cloud.sso.web;

import com.cloud.sso.common.constant.OperateType;
import com.cloud.sso.common.exception.AppException;
import com.cloud.sso.common.log.annoatation.BusinessLog;
import com.cloud.sso.common.page.AppPageInfo;
import com.cloud.sso.common.result.Result;
import com.cloud.sso.dto.*;
import com.cloud.sso.service.RoleService;
import com.cloud.sso.vo.UserRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.cloud.sso.common.result.Result.success;

/**
 * 角色处理类
 */
@RequestMapping("/api/role")
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 分页查询 *
     *
     * @param queryDto
     * @return
     */
    @RequestMapping("/query")
    public Result<AppPageInfo> queryByPage(@RequestBody RoleQueryDto queryDto) {
        return Result.success(roleService.query(queryDto));
    }

    /**
     * 查询用户角色 *
     *
     * @param queryDto
     * @return
     */
    @RequestMapping("/user/role")
    public Result<List<UserRoleVo>> query(@RequestBody UserRoleQueryDto queryDto) {
        return Result.success(roleService.queryUserRole(queryDto));
    }

    /**
     * 查询角色授权人员-分页
     *
     * @param queryDto
     * @return
     */
    @RequestMapping("/user/role/page")
    public Result<AppPageInfo> queryUserRolePage(@RequestBody UserRoleQueryDto queryDto) {
        return Result.success(roleService.queryUserRolePage(queryDto));
    }

    /**
     * 保存用户授权信息 *
     *
     * @param userRoleDto
     * @return
     */
    @BusinessLog(value = "用户授权", module = "角色管理",field = "userId", type = OperateType.INSERT)
    @RequestMapping("/user/role/save")
    public Result saveUserRole(@RequestBody @Validated UserRoleDto userRoleDto, BindingResult result) {
        if (result.hasErrors()) {
            throw new AppException(result.getFieldError().getDefaultMessage());
        }
        roleService.saveUserRole(userRoleDto);
        return success(1);
    }

    /**
     * 解除用户角色绑定 *
     *
     * @param id
     * @return
     */
    @BusinessLog(value = "解除用户角色授权", module = "角色管理", field = "id", type = OperateType.DELETE)
    @RequestMapping("/user/role/delete")
    public Result deleteUserRole(@RequestParam String id) {
        roleService.deleteUserRole(id);
        return success(1);
    }

    /**
     * 角色绑定菜单  *
     *
     * @param saveDto
     * @return
     */
    @RequestMapping("/menu/save")
    public Result menuSave(@RequestBody @Validated RoleMenuSaveDto saveDto, BindingResult result) {
        if (result.hasErrors()) {
            throw new AppException(result.getFieldError().getDefaultMessage());
        }
        roleService.saveRoleMenu(saveDto);
        return Result.success(1);
    }

    /**
     * 解除角色菜单绑定关系 *
     *
     * @return
     */
    @RequestMapping("/menu/delete")
    public Result deleteRoleMenu(@RequestParam String menuId) {
        roleService.deleteRoleMenu(menuId);
        return Result.success(1);
    }

    /**
     * 获取角色已经绑定的菜单
     *
     * @param roleId
     * @return
     */
    @RequestMapping("/binded/menus")
    public Result getBindMenus(@RequestParam String roleId) {
        return Result.success(roleService.queryByRole(roleId));
    }

    /**
     * 保存角色 *
     *
     * @param saveDto
     * @return
     */
    @BusinessLog(value = "新增角色", module = "角色管理", field = "roleName", type = OperateType.INSERT)
    @RequestMapping("/save")
    public Result save(@RequestBody @Validated RoleSaveDto saveDto, BindingResult result) {
        if (result.hasErrors()) {
            throw new AppException(result.getFieldError().getDefaultMessage());
        }
        roleService.save(saveDto);
        return Result.success(1);
    }

    /**
     * 修改保存 *
     *
     * @param saveDto
     * @return
     */
    @BusinessLog(value = "修改角色", module = "角色管理", field = "roleName", type = OperateType.UPDATE)
    @RequestMapping("/update")
    public Result update(@RequestBody RoleSaveDto saveDto) {
        roleService.save(saveDto);
        return Result.success(1);
    }

    /**
     * 删除角色 *
     *
     * @param id
     * @return
     */
    @BusinessLog(value = "删除角色", module = "角色管理", field = "id", type = OperateType.DELETE)
    @RequestMapping("/delete")
    public Result delete(@RequestParam String id) {
        roleService.delete(id);
        return Result.success(1);
    }

    /**
     * 查询角色详情 *
     *
     * @param id
     * @return
     */
    @RequestMapping("/get")
    public Result get(@RequestParam String id) {
        return Result.success(roleService.get(id));
    }
}
